﻿namespace YidanSoft.Library.EditorUtility.Collections
{
    using System;
    using System.Collections;
    using System.Reflection;
    using System.Threading;
    using YidanSoft.Library.EditorUtility.WinControls;

    public class OutlookBarBandCollection : CollectionBase, IEnumerable
    {
        private EventHandler Changed;
        private OutlookBar parentBar = null;

        public event EventHandler Changed
        {
            add
            {
                EventHandler handler2;
                EventHandler changed = this.Changed;
                do
                {
                    handler2 = changed;
                    EventHandler handler3 = (EventHandler) Delegate.Combine(handler2, value);
                    changed = Interlocked.CompareExchange<EventHandler>(ref this.Changed, handler3, handler2);
                }
                while (changed != handler2);
            }
            remove
            {
                EventHandler handler2;
                EventHandler changed = this.Changed;
                do
                {
                    handler2 = changed;
                    EventHandler handler3 = (EventHandler) Delegate.Remove(handler2, value);
                    changed = Interlocked.CompareExchange<EventHandler>(ref this.Changed, handler3, handler2);
                }
                while (changed != handler2);
            }
        }

        public OutlookBarBandCollection(OutlookBar bar)
        {
            this.parentBar = bar;
        }

        public int Add(OutlookBarBand band)
        {
            if (this.Contains(band))
            {
                return -1;
            }
            int num = base.InnerList.Add(band);
            this.RaiseChanged();
            return num;
        }

        public bool Contains(OutlookBarBand band)
        {
            return base.InnerList.Contains(band);
        }

        public int IndexOf(OutlookBarBand band)
        {
            return base.InnerList.IndexOf(band);
        }

        private void RaiseChanged()
        {
            if (this.Changed != null)
            {
                this.Changed(this, null);
            }
        }

        public void Remove(OutlookBarBand band)
        {
            int currentBand = this.parentBar.GetCurrentBand();
            bool flag = (currentBand != -1) && (currentBand == (base.Count - 1));
            base.InnerList.Remove(band);
            if (flag && (base.Count > 0))
            {
                this.parentBar.SetCurrentBand(base.Count - 1);
            }
            this.RaiseChanged();
        }

        public OutlookBarBand this[int index]
        {
            get
            {
                if ((index < 0) || (index >= base.Count))
                {
                    return null;
                }
                return (OutlookBarBand) base.InnerList[index];
            }
        }
    }
}

